let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let startParamObj = wx.getLaunchOptionsSync(); 
        console.log('startParamObj=>'+JSON.stringify(startParamObj));
        var enterOptions =wx.getEnterOptionsSync();
        console.log(enterOptions);
        var path = enterOptions.path;
        if(path == "pages/login/index"){
            path="pages/index/index";
        }
        this.setData({
            path:path
        });
        console.log(path);
        wx.setStorageSync('tabIndex',0);   
        let that = this;
        this.getMenu();
        //this.getOpen();
        app.autoUpdate().then(res=>{ 
            // console.log(11); 
            /*
             console.log(res);
             wx.reLaunch({
               url: '/pages/index/index',
             })
             */
             that.getOpen(); //that.wxLogIn();  
          })
    },
    getMenu:function(){
        let that = this;
        var version = this.getVersion();
        console.log(version);
        wx.request({
          url: app.globalData.domain+'/wechat/login/getMenu',
          data:{from:1,version:version},
          success:res=>{
              console.log(res.data);
            //  that.setPageNavbar();
            var tab = res.data.menu;
            var city = res.data.city;  
            let d = res.data;
            wx.setStorageSync('tabBar',d.menu);
            wx.setStorageSync('city', d.city)
            wx.setStorageSync('rent_type',d.rent_type);
            /*
              tabService.updateTabArray(this,tab);
              var tabIndex = wx.getStorageSync('tabIndex');
              var role=0;
              tabService.updateRole(this,role)
              tabService.updateIndex(this, tabIndex);
              */
          }
      })
      },
   getVersion() {
        console.log('__wxConfig.envVersion:', __wxConfig.envVersion);
        let version = __wxConfig.envVersion;
        return version;
    }, 
    getOpen(){
        let that=this;
        var open=wx.getStorageSync('open'); 
        var parent_id=wx.getStorageSync('parent_id');
        var store_id=wx.getStorageSync('store_id'); 
        console.log(open);
        if(open!="" && open!=undefined){
            wx.request({
                url: app.globalData.domain+'/wechat/login/getUser',
                data:{id:open.id,from:3,parent_id:parent_id,store_id:store_id},
                success:res=>{        
                  console.log(res);
                  if(res.statusCode==200){
                      console.log(res.statusCode);
                    
                    var roles=res.data.roles;
                    wx.setStorageSync('role',roles); 
                 //  wx.setStorageSync('user',res.data.user); 
                    wx.setStorageSync('open',res.data.open); 
                   that.showAdvert(); 
                  }else{
                      console.log(res.statusCode);
                    that.getUnionId();
                  }                             
                }
              })   
              wx.reLaunch({
                url: '/pages/index/index',
              })
      
        }else{     
            console.log('no info')      
            that.getUnionId();
             
    
            
          
            }
      },
      getUnionId(){
          let that = this;
        wx.login({
            success: res => {
          var code = res.code; 
          console.log('code='+code)         
         
             wx.request({
                url: app.globalData.domain+'/wechat/login/getUnionId',
                data: { code: code },
                success: (res) => {
                  console.log(res.data);
                  //console.log(res.data.unionid);
                 // wx.setStorageSync('open', res.data)
                 var data=res.data;
                 if(data.open.id){
                   wx.setStorageSync('open', data.open);        
                } 
                            
                that.showAdvert();
               
                }
              })
            }})
      },
      getLoginImage:function(){
        let that = this;
        wx.request({
          url: app.globalData.platUrl+'/wechat/login/getLoginImage',
          data:{from:1},
          success:res=>{
            that.setData({
              loginImage:res.data.image
            })
          }
      })
      },
      showAdvert:function(){
        var parent_id=wx.getStorageSync('parent_id');
        var store_id=wx.getStorageSync('store_id');
        var path = this.data.path;
        console.log(path);
        console.log('store_id='+store_id)
        if(store_id==undefined || store_id ==''){
            store_id=0; 
            var url = '/'+path+'?parent_id='+parent_id+'&store_id='+store_id;          
            console.log(url);
                wx.reLaunch({
                    url: url,
                });
        }else{
          path="pages/index/index";
          var url = '/'+path+'?parent_id='+parent_id+'&store_id='+store_id;
          wx.reLaunch({             
            url: url,
          });
        }
          
    
        this.setData({
          showAdTime:true
        });
        let that = this;
        var timers;
        //that.data.timers = setInterval(that.toTimer,1000);   
  
       　
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})